package org.tea.application.browser.handler;

import org.cef.browser.CefBrowser;
import org.cef.browser.CefFrame;
import org.cef.callback.CefAuthCallback;
import org.cef.callback.CefCallback;
import org.cef.handler.*;
import org.cef.misc.BoolRef;
import org.cef.misc.StringRef;
import org.cef.network.CefRequest;
import org.cef.network.CefResponse;
import org.cef.network.CefURLRequest;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.HashMap;
import java.util.Map;

/**
 * @author lxh
 */
public class LoggingRequestHandler
        implements CefRequestHandler, CefResourceRequestHandler {

    private final static Logger log = LoggerFactory.getLogger(LoggingRequestHandler.class);

    public LoggingRequestHandler() {
    }

    @Override
    public boolean onBeforeBrowse(CefBrowser browser,
                                  CefFrame frame,
                                  CefRequest request,
                                  boolean user_gesture,
                                  boolean is_redirect) {
        return false;
    }

    @Override
    public boolean onOpenURLFromTab(CefBrowser browser,
                                    CefFrame frame,
                                    String target_url,
                                    boolean user_gesture) {
        return false;
    }

    @Override
    public CefResourceRequestHandler
    getResourceRequestHandler(CefBrowser browser,
                              CefFrame frame,
                              CefRequest request,
                              boolean isNavigation,
                              boolean isDownload,
                              String requestInitiator,
                              BoolRef disableDefaultHandling) {
        return this;
    }

    @Override
    public CefCookieAccessFilter getCookieAccessFilter(CefBrowser browser,
                                                       CefFrame frame,
                                                       CefRequest request) {
        return null;
    }

    @Override
    public boolean onBeforeResourceLoad(CefBrowser browser,
                                        CefFrame frame,
                                        CefRequest request) {
        Map<String, String> headers = new HashMap<>();
        request.getHeaderMap(headers);
        request.setHeaderMap(headers);
        return false;
    }

    @Override
    public CefResourceHandler getResourceHandler(CefBrowser browser,
                                                 CefFrame frame,
                                                 CefRequest request) {
        return null;
    }

    @Override
    public void onResourceRedirect(CefBrowser browser,
                                   CefFrame frame,
                                   CefRequest request,
                                   CefResponse response,
                                   StringRef new_url) {

    }

    @Override
    public boolean getAuthCredentials(CefBrowser browser,
                                      String origin_url,
                                      boolean isProxy,
                                      String host,
                                      int port, String realm,
                                      String scheme,
                                      CefAuthCallback callback) {
        return false;
    }

    @Override
    public boolean onCertificateError(CefBrowser browser,
                                      CefLoadHandler.ErrorCode cert_error,
                                      String request_url,
                                      CefCallback callback) {
        return false;
    }

    @Override
    public void onRenderProcessTerminated(CefBrowser cefBrowser,
                                          TerminationStatus terminationStatus,
                                          int i, String s) {

    }

    @Override
    public boolean onResourceResponse(CefBrowser browser,
                                      CefFrame frame,
                                      CefRequest request,
                                      CefResponse response) {
        return false;
    }

    @Override
    public void onResourceLoadComplete(CefBrowser browser,
                                       CefFrame frame,
                                       CefRequest request,
                                       CefResponse response,
                                       CefURLRequest.Status status,
                                       long receivedContentLength) {
    }

    @Override
    public void onProtocolExecution(CefBrowser browser,
                                    CefFrame frame,
                                    CefRequest request,
                                    BoolRef allowOsExecution) {
    }
}
